<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class HealthRecords extends Model
{
    protected $table = 'health_records';
    public $timestamps = false;


    const DEL_OFF = 1;
    const DEL_ON = 0;

    //审核状态
    const STATUS_WAITING = 1;
    const STATUS_YES = 2;
    const STATUS_REFUSE = 3;
    const STATUS_NEW = 4;
    const STATUS_NEW_AGREE = 5;
    const STATUS_NEW_REFUSE = 6;


    static $StatusDisplay = [
        self::STATUS_WAITING => '待审核',
        self::STATUS_YES => '审核通过',
        self::STATUS_REFUSE => '审核失败',
        self::STATUS_NEW => '新信息审核中',
        self::STATUS_NEW_AGREE => '新信息通过',
        self::STATUS_NEW_REFUSE => '新信息未通过',
    ];

    //是否完整
    const COMPLETE_NO = 0;
    const COMPLETE_YES = 1;


    /**
     * 通过审核
     * @param $healthRecords
     * @return array
     * @throws \Exception
     */
    static function StatusOn($healthRecords)
    {
        $content = "亲爱的用户您好，您的健康档案已通过审核！";
        DB::beginTransaction();

        //1.修改状态
        $healthRecords->status = self::STATUS_YES;
        if (!$healthRecords->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }
        //2.奖励抽奖次数和金币
        $luckdraw = self::GiveLuckDraw($healthRecords);
        if ($luckdraw['status'] !== 1) {
            DB::rollBack();

            return $luckdraw;
        }
        if ($healthRecords->is_complete === self::COMPLETE_YES) {
            $coins = self::GiveCoins($healthRecords);
            if ($coins['status'] !== 1) {
                DB::rollBack();

                return $coins;
            }
        }
        if ($luckdraw['message']) {
            $content = "亲爱的用户您好，您的健康档案已通过审核，恭喜您获得一次抽奖机会，您可以在收益中心查看";
            if (isset($coins) && $coins['message']) {
                $content = "亲爱的用户您好，您的健康档案已通过审核，恭喜您获得200金币奖励和一次抽奖机会";
            }
        }

        //添加系统消息
        $user_id = User::where('uuid', $healthRecords->user_uuid)->value('c_id');


        $message = Curl::PushOneMessage($user_id, $content);
        if ($message['status'] !== 1) {
            DB::rollBack();

            return $message;
        }

        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 新信息通过审核
     * @param $healthRecords
     * @return array
     * @throws \Exception
     */
    static function NewStatusOn($healthRecords)
    {
        DB::beginTransaction();
        $content = "亲爱的用户您好，您的健康档案已通过审核！";
        $new = DB::table('foll_health_records')->where('id', $healthRecords->foll_id)->first();

        //1.奖励金币   如果新信息的is_complete为1  且原信息的is_complete为0  则有金币奖励
        if ($healthRecords->is_complete === self::COMPLETE_NO && $new->is_complete === self::COMPLETE_YES) {
            $coins = self::GiveCoins($healthRecords);
            if ($coins['status'] !== 1) {
                DB::rollBack();

                return $coins;
            }
            if ($coins['message']) {
                $content = "亲爱的用户您好，您的健康档案已通过审核，恭喜您获得200金币奖励，您可以在收益中心查看！";
            }
        }
        //2.修改信息
        $healthRecords->age = $new->age;
        $healthRecords->name = $new->name;
        $healthRecords->relationship_value = $new->relationship_value;
        $healthRecords->sex = $new->sex;
        $healthRecords->relationship = $new->relationship;
        $healthRecords->birthday = $new->birthday;
        $healthRecords->telephone = $new->telephone;
        $healthRecords->history_disease_value = $new->history_disease_value;
        $healthRecords->history_disease_name = $new->history_disease_name;
        $healthRecords->address = $new->address;
        $healthRecords->address_detail = $new->address_detail;
        $healthRecords->allergy = $new->allergy;
        $healthRecords->hospital = $new->hospital;
        $healthRecords->clinic = $new->clinic;
        $healthRecords->pharmacy = $new->pharmacy;
        $healthRecords->diseases = $new->diseases;
        $healthRecords->medicines = $new->medicines;
        $healthRecords->is_complete = $new->is_complete;
        $healthRecords->address_p_value = $new->address_p_value;
        $healthRecords->address_c_value = $new->address_c_value;
        $healthRecords->address_d_value = $new->address_d_value;
        $healthRecords->status = self::STATUS_NEW_AGREE;
        if (!$healthRecords->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }

        //添加系统消息
        $user_id = User::where('uuid', $healthRecords->user_uuid)->value('c_id');

//        $content = "亲爱的用户您好，您的健康档案已通过审核，恭喜您获得200金币奖励，您可以在收益中心查看！";
        $message = Curl::PushOneMessage($user_id, $content);
        if ($message['status'] !== 1) {
            DB::rollBack();

            return $message;
        }

        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 奖励抽奖次数
     * @param $healthRecords
     * @return array
     */
    static function GiveLuckDraw($healthRecords)
    {
        $user_id = User::where('uuid', $healthRecords->user_uuid)->value('c_id');
        //1.是否领过该奖励
        if (HealthRecords::where('user_uuid', $healthRecords->user_uuid)
                ->whereNotIn('status', [self::STATUS_WAITING, self::STATUS_REFUSE])
                ->count() > 1) {
            return ['status' => 1, 'message' => 0];
        }
        //2.发放奖励
        if (DB::table('rel_member_lucky')->where('user_id', $user_id)->exists()) {
            $luckdraw = DB::table('rel_member_lucky')->where('user_id', $user_id)->increment('lucky_opportunity');
        } else {
            $luckdraw = DB::table('rel_member_lucky')->insert([
                'user_id' => $user_id,
                'lucky_opportunity' => 1,
                'lucky_value' => 0,
            ]);
        }

        if (!$luckdraw) {
            return ['status' => 2, 'message' => '奖励抽奖次数失败'];
        }
        return ['status' => 1, 'message' => 1];
    }

    /**
     * 奖励金币
     * @param $healthRecords
     * @return array
     */
    static function GiveCoins($healthRecords)
    {
        //1.是否领过该奖励
        if (HealthRecords::where('user_uuid', $healthRecords->user_uuid)
                ->where('is_complete', self::COMPLETE_YES)
                ->whereNotIn('status', [self::STATUS_WAITING, self::STATUS_REFUSE])
                ->count() > 1) {
            return ['status' => 1, 'message' => 0];
        }
        //2.发放奖励
        if (!User::where('uuid', $healthRecords->user_uuid)->increment('coin_number', 200)) {
            return ['status' => 2, 'message' => '奖励金币失败'];
        }
        return ['status' => 1, 'message' => 1];
    }


    /**
     * 选填审核不通过
     * @param $healthRecords
     * @return array
     * @throws \Exception
     */
    static function NewStatusOff($healthRecords)
    {
        DB::beginTransaction();

        //修改状态
        $healthRecords->status = HealthRecords::STATUS_NEW_REFUSE;
        if (!$healthRecords->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '审核失败'];
        }

        //添加系统消息
        $user_id = User::where('uuid', $healthRecords->user_uuid)->value('c_id');

        $content = "亲爱的用户您好，经后台审核，您的健康档案填写有误，请重新填写，谢谢合作！";
        $message = Curl::PushOneMessage($user_id, $content);
        if ($message['status'] !== 1) {
            DB::rollBack();

            return $message;
        }


        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 不通过审核
     * @param $healthRecords
     * @return array
     * @throws \Exception
     */
    static function StatusOff($healthRecords)
    {
        DB::beginTransaction();

        //修改状态
        $healthRecords->status = HealthRecords::STATUS_REFUSE;
        if (!$healthRecords->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '审核失败'];
        }

        //添加系统消息
        $user_id = User::where('uuid', $healthRecords->user_uuid)->value('c_id');

        $content = "亲爱的用户您好，经后台审核，您的健康档案填写有误，请重新填写，谢谢合作！";
        $message = Curl::PushOneMessage($user_id, $content);
        if ($message['status'] !== 1) {
            DB::rollBack();

            return $message;
        }


        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }
}
